//
//  InfoView.m
//  AoJi
//
//  Created by IMacLife on 15/1/30.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//
//基本信息

#import "InfoView.h"
#import "Color.h"

@implementation InfoView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataSource = [NSArray array];
        [self loadView];
        [self loadData];
    }
    return self;
}

//加载view
-(void)loadView
{
    _tableview = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.backgroundColor=[UIColor clearColor];
    [self addSubview:_tableview];
    
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor=[Color colorFromHexRGB:@"e4e4e4"];
    _tableview.tableFooterView=footView;
}

-(void)loadData
{
    
    NSString *userName = [kUserDefaults objectForKey:kUserName];
    NSString *mobile = [kUserDefaults objectForKey:kMobile];
    NSString *email = [kUserDefaults objectForKey:kEmail];
    NSString *address = [kUserDefaults objectForKey:kAddress];
    NSString *registerDate = [kUserDefaults objectForKey:kRegisterDate];
    
    //日期转换
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[registerDate intValue]];
    registerDate=[formatter stringFromDate:confromTimesp];
    
    _dataSource = @[userName,mobile,email,address,registerDate];
    [_tableview reloadData];
    
    
    /**
     *  个人信息(ipad)
     *
     *  @param response 请求个人信息
     *
     *  @return 两栏都有
     */
//    [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:nil WithApi:@"me/profile" WhenSuccess:^(NSMutableDictionary *response) {
//        NSLog(@"%@",response);
//        NSArray *array =response;
//        NSLog(@"%@",array[0][@"ItemTitle"]);
//    } WhenFailure:^(NSError *failure) {
//        NSLog(@"%@",failure);
//    }];
}

#pragma mark ----------------UITableViewDelegate
//一个分区显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}


//UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSArray *leftArray = @[@"用户昵称",@"电  话  ",@"E-mail  ",@"用户住址",@"注册日期"];
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 100, 20)];
    leftLabel.text=leftArray[indexPath.row];
    leftLabel.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:leftLabel];
    
    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftLabel.frame)+50, 10, [UIScreen mainScreen].bounds.size.width-CGRectGetMaxX(leftLabel.frame)-60, 20)];
    rightLabel.text=[NSString stringWithFormat:@"%@",_dataSource[indexPath.row]];
    rightLabel.textAlignment=NSTextAlignmentRight;
    rightLabel.textColor=[Color colorFromHexRGB:@"686868"];
    //[rightLabel sizeToFit];
    [cell.contentView addSubview:rightLabel];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //手机号码中间星号显示
    
    if(![rightLabel.text isEqualToString:@""]){
    
    if (indexPath.row == 1) {
        NSString *str = [rightLabel.text stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        rightLabel.text=str;
      }
    }
    
    return cell;
}



@end
